package entry76;

import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Date;

public class Period implements Serializable {
	private static final long serialVersionUID = -635058698980457102L;

	private Date start, end;

	public Period(Date start, Date end) {
		this.start = new Date(start.getTime());
		this.end = new Date(end.getTime());

		if (isValid()) {
			throw new IllegalArgumentException(start + " after " + end);
		}
	}

	private void readObject(ObjectInputStream s) throws IOException,
			ClassNotFoundException {
		s.defaultReadObject();
		this.start = new Date(start.getTime());
		this.end = new Date(end.getTime());

		if (start.compareTo(end) > 0) {
			throw new InvalidObjectException(start + " after " + end);
		}
	}

	public boolean isValid() {
		return (start.compareTo(end) < 0);
	}

	@Override
	public String toString() {
		return "Period [start=" + start + ", end=" + end + "]";
	}
}